package com.javashow.main.factory.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javashow.main.factory.dao.IModelFactoryDao;
import com.javashow.main.factory.service.IModelFactoryService;
import com.javashow.pojo.DataModelFields;

@Service(value = "modelFactoryService")
public class ModelFactoryServiceImpl implements IModelFactoryService {

	@Autowired
	private IModelFactoryDao modelFactoryDao;
	
	@Override
	public JSONArray getModelByName(String modelName) {
		JSONArray result = new JSONArray();
		
		List<DataModelFields> list = modelFactoryDao.queryModelByName(modelName);
		
		for(DataModelFields item : list) {
			result.add(getSingleField(item));
		}
		
		return result;
	}

	private JSONObject getSingleField(DataModelFields item) {
		JSONObject result = new JSONObject();
		
		if(item.getDataConvert() != null && !"".equals(item.getDataConvert())) {
			result.accumulate("convert", item.getDataConvert());
		}
		
		if(item.getDateFormat() != null && !"".equals(item.getDateFormat())) {
			result.accumulate("dateFormat", item.getDateFormat());
		}
		
		if(item.getDefaultValue() != null && !"".equals(item.getDefaultValue())) {
			result.accumulate("defaultValue", item.getDefaultValue());
		}
		
		if(item.getMapping() != null && !"".equals(item.getMapping())) {
			result.accumulate("mapping", item.getMapping());
		}
		
		if(item.getName() != null && !"".equals(item.getName())) {
			result.accumulate("name", item.getName());
		}
		
		if(item.getPersist() != null && !"".equals(item.getPersist())) {
			result.accumulate("persist", item.getPersist());
		}
		
		if(item.getSerialize() != null && !"".equals(item.getSerialize())) {
			result.accumulate("serialize", item.getSerialize());
		}
		
		if(item.getSortDir() != null && !"".equals(item.getSortDir())) {
			result.accumulate("sortDir", item.getSortDir());
		}
		
		if(item.getSortType() != null && !"".equals(item.getSortType())) {
			result.accumulate("sortType", item.getSortType());
		}
		
		if(item.getType() != null && !"".equals(item.getType())) {
			result.accumulate("type", item.getType());
		}
		
		if(item.getUserNull() != null && !"".equals(item.getUserNull())) {
			result.accumulate("useNull", item.getUserNull());
		}
		
		return result;
	}
}
